package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.ImpSchemeService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class ImpSchemeController {
	private static final Logger logger = LoggerFactory.getLogger(ImpSchemeController.class);

	@Autowired
	private ImpSchemeService impSchemeService;
	
	@RequestMapping(value = "/getImpSchemeList")
	public void getImpSchemeList(ExtPager page,HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		HashMap<String,Object> param = new HashMap<String,Object>();
		HashMap<String,Object> result = new HashMap<String,Object>();
		response.setContentType("text/plain;charset=UTF-8");
		try {
			String language = (String) session.getAttribute(WebConstants.CURRENT_LANG);
			ArrayList<Map<String,String>> field = page.getSortField();
			StringBuffer sort = new StringBuffer("");	
				param.put("pageEnd",page.getStart() + page.getLimit());
				if(field!=null && field.size()>0){
					for(Map<String, String> map : field){
			        	sort.append(map.get("property") + " " + map.get("direction") + ",");
			        }
					param.put("orderBy", sort.substring(0,sort.length()-1));
				} else  {
					param.put("orderBy", "FILES_SCHEME_HID");
				}
				param.put("language", language);
				param.put("pageStart", page.getStart());									
			ArrayList<HashMap<String,Object>> logs = impSchemeService.getImpSchemeList(param);
			if(logs!=null&&logs.size()>0){
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", logs);
			} else {
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", "");
			}
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);		
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
	
	@RequestMapping(value = "/setQuoteImpscheme",method = RequestMethod.POST)
	public void setQuoteImpscheme(HttpSession session,HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		String schemeId = (String)request.getParameter("schemeId") == null ? null : (String)request.getParameter("schemeId");
		HashMap<String,Object> result = new HashMap<String,Object>();
		response.setContentType("text/plain;charset=UTF-8");		
		try {	
			String company_id = session.getAttribute(WebConstants.CURRENT_COMPANY).toString();
			User user = (User)session.getAttribute(WebConstants.CURRENT_USER);
			if(user != null){
				HashMap<String, Object> param = new HashMap<String, Object>();				
				param.put("scheme_id", schemeId);
				param.put("company_id", company_id);
				int ret = impSchemeService.setQuoteImpscheme(param);
				if(ret == 0){
					result.put("resultcode",0);
				}else{
					result.put("resultcode",9);
				}
			}else{
		 		writer.print("");
			  }
			writer.print(JackJson.fromObjectToJson(result));
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	 }
}